﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZilLion.Service.Core.Domain.EntityBase;
using ZilLion.Service.Core.Infrastructure.Base;
using ZilLion.Service.TaskPlatform.Controllers.StateContainer;
using ZilLion.Service.TaskPlatform.Domain.Entity;
using ZilLion.Service.TaskPlatform.Domain.Response;
using ZilLion.Service.TaskPlatform.Domain.Respository.Interface;
using ZilLion.Service.TaskPlatform.Domain.TaskCommand;

namespace ZilLion.Service.TaskPlatform.Controllers
{
    /// <summary>
    ///     任务配置
    /// </summary>
    [Route("Api/TaskPlatform/TaskConfig")]
    [AllowAnonymous]
    public class TaskConfigController : ZilLionControllerBase
    {
        private readonly ITaskConfigRespository _taskConfigRespository;
        private readonly ITaskModuleConfigRespository _taskModuleConfigRespository;

        private readonly TaskRunStateContainer _taskRunStateContainer;
        private readonly ITaskStateRespository _taskStateRespository;

        public TaskConfigController(ITaskModuleConfigRespository taskModuleConfigRespository,
            ITaskConfigRespository taskConfigRespository, ITaskStateRespository taskStateRespository,
            TaskRunStateContainer taskRunStateContainer)
        {
            _taskModuleConfigRespository = taskModuleConfigRespository;
            _taskConfigRespository = taskConfigRespository;
            _taskStateRespository = taskStateRespository;
            _taskRunStateContainer = taskRunStateContainer;
        }

        [HttpGet("GetAllTaskConfig")]
        public TaskConfigResponse GetAllTaskConfig()
        {
            var list = _taskConfigRespository.GetjobConfigs();
            return new TaskConfigResponse {TaskConfigList = list};
        }


        [HttpGet("GetTaskConfig")]
        public TaskConfigResponse GetTaskConfig(string taskid)
        {
            var config = _taskConfigRespository.GetjobConfigById(taskid);
            return new TaskConfigResponse {TaskConfigList = new List<TaskConfig> {config}};
        }


        [HttpPost("SaveTaskConfig")]
        public ResponseBase SaveTaskConfig([FromBody] TaskConfig config)
        {
            config.UpdateTime = DateTime.UtcNow.AddHours(8);
            var oldconfig = _taskConfigRespository.GetjobConfigById(config.Taskid);
            if (oldconfig != null)
                _taskConfigRespository.MotifyData(config);
            else
                _taskConfigRespository.AddData(config);

            #region 重启所有节点的task

            _taskRunStateContainer.FlushStates(); //先持久化所有任务状态
            var states = _taskStateRespository.GetAllTaskState()
                .Where(x => x.Taskid == config.Taskid && x.Taskstatus == 0).ToList();

            foreach (var state in states)
                TaskCommandHelper.ReStartTask(state.NodeId, state.Taskid);

            #endregion

            return new ResponseBase();
        }


        [HttpGet("GetTaskModuleConfig")]
        public TaskModuleConfigResponse GetTaskModuleConfig(string moduleid)
        {
            var config = _taskModuleConfigRespository.GetModuleConfigById(moduleid);
            return new TaskModuleConfigResponse {TaskModuleList = new List<TaskModuleConfigEntity> {config}};
        }


        [HttpGet("GetAllTaskModuleConfig")]
        public TaskModuleConfigResponse GetAllTaskMmoduleConfig()
        {
            var config = _taskModuleConfigRespository.GetAllModuleConfigs();
            return new TaskModuleConfigResponse {TaskModuleList = config};
        }

        [HttpPost("SaveModuleConfig")]
        public ResponseBase SaveModuleConfig([FromBody] TaskModuleConfigEntity config)
        {
            var oldconfig = _taskModuleConfigRespository.GetModuleConfigById(config.TaskmoduleId);
            if (oldconfig != null)
                _taskModuleConfigRespository.MotifyModuleConfig(config);
            else
                _taskModuleConfigRespository.AddModuleConfig(config);


            #region 重启所有节点的task

            var tasks = _taskConfigRespository.GetjobConfigs().Where(x => x.TaskmoduleId == config.TaskmoduleId)
                .Select(x => x.Taskid);
            _taskRunStateContainer.FlushStates(); //先持久化所有任务状态

            foreach (var taskid in tasks)
            {
                var states = _taskStateRespository.GetAllTaskState()
                    .Where(x => x.Taskid == taskid && x.Taskstatus == 0).ToList();
                foreach (var state in states)
                    TaskCommandHelper.ReStartTask(state.NodeId, state.Taskid);
            }

            #endregion

            return new ResponseBase();
        }

        [HttpDelete("DeleteModuleConfig")]
        public ResponseBase DeleteModuleConfig(string mouduleid)
        {
            _taskModuleConfigRespository.RemoveModule(mouduleid);
            return new ResponseBase();
        }
    }
}